package org.example.spring_book.controller;

 /**
 * @ClassName UserController
 * @Description TODO
 * @Author wangxiaodong
 */

import org.example.spring_book.constants.Constants;
import org.example.spring_book.model.UserInfo;
import org.example.spring_book.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session){
        //校验参数
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)){
            return false;
        }
        //判断数据库的用户密码和输入的是否一致
        //查询数据库
        UserInfo userInfo=userService.queryByName(userName);
        if(userInfo==null){
            return false;
        }
        if(password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute(Constants.USER_SESSION_KEY,userInfo);
            return true;
        }
        return false;
    }
}
